package cn.edu.java.stream.特殊流;

import java.util.stream.DoubleStream;

public class DoubleStreamDemo {
    public static void main(String[] args) {
        // 创建一个 DoubleStream 流
        DoubleStream stream = DoubleStream.of(1.2, 3.4, 5.6, 7.8, 9.0);

        // 打印流中的元素
        stream.forEach(System.out::println);

        // 求和
        double sum = DoubleStream.of(1.2, 3.4, 5.6, 7.8, 9.0)
                .sum();
        System.out.println("Sum: " + sum);

        // 平均值
        double average = DoubleStream.of(1.2, 3.4, 5.6, 7.8, 9.0)
                .average()
                .orElse(0.0);
        System.out.println("Average: " + average);

        // 最大值
        double max = DoubleStream.of(1.2, 3.4, 5.6, 7.8, 9.0)
                .max()
                .orElse(0.0);
        System.out.println("Max: " + max);

        // 过滤并打印偶数
        DoubleStream.of(1.2, 3.4, 5.6, 7.8, 9.0)
                .filter(n -> n % 2 == 0)
                .forEach(System.out::println);
    }
}

